#!/bin/sh
#
# This is the script for generating files for a specific Dalton test job.
#
# For the .check file ksh or bash is preferred, otherwise use sh
# (and hope it is not the old Bourne shell, which will not work)
#
if [ -x /bin/ksh ]; then
   CHECK_SHELL='#!/bin/ksh'
elif [ -x /bin/bash ]; then
   CHECK_SHELL='#!/bin/bash'
else
   CHECK_SHELL='#!/bin/sh'
fi


#######################################################################
#  TEST DESCRIPTION
#######################################################################
cat > cc_rsp_gammadirect.info <<'%EOF%'
   cc_rsp_gammadirect
   ------------------
   Molecule:         HF in C2
   Wave Function:    CCSD / 6-31G**
   Test Purpose:     Second hyperpolarizability (cubic response):
                     - Parallel isotropic average (gamma_|| ) 
                     - A & B coefficients  (2. and 4. order disp.
                       coefficients)
                     - Explicitly use symmetry of linear molecules
%EOF%

#######################################################################
#  INTEGRAL INPUT
#######################################################################
cat > cc_rsp_gammadirect.mol <<'%EOF%'
BASIS
6-31G**
Hydrogen flourid
small test basis
    2    1  Y  X      1.00D-15
        9.0   1    
F     0.0000000000000000  0.0000000000000000 1.76320000000000000      
        1.0   1
H     0.0000000000000000  0.0000000000000000 0.00000000000000000      
%EOF%

#######################################################################
#  DALTON INPUT
#######################################################################
cat > cc_rsp_gammadirect.dal <<'%EOF%'
**DALTON INPUT
.RUN WAVE FUNCTIONS
.DIRECT
**INTEGRAL
.DIPLEN
**WAVE FUNCTIONS
.TITLE
  second hyperpolarizability for the HF molecule
  for a CCS wavefunction compute:
  - parallel isotropic average (gamma_|| ) 
  - A & B coefficients  (2. and 4. order disp. coefficients)
  - use explicitly symmetry of linear molecules
.CC
*SCF INPUT 
.THRESH
 1.0d-12
*CC INPUT 
.CCSD
.THRENR
 1.0d-10
.THRLEQ
 1.0d-10
*CCLR
.OLD_LR
.DIPOLE
*CCQR
.AVERAGE
HYPERPOLARIZABILITY
*CCCR
.AVERAGE
GAMMA_PAR            
LINEAR
.DISPCF
  2
**END OF DALTON INPUT
%EOF%
#######################################################################

 

#######################################################################

#######################################################################
#  CHECK SCRIPT
#######################################################################
echo $CHECK_SHELL >cc_rsp_gammadirect.check
cat >>cc_rsp_gammadirect.check <<'%EOF%'
log=$1
treshhold=1.0D-8

if [ `uname` = Linux ]; then
   GREP="egrep -a"
else
   GREP="egrep"
fi

if $GREP -q "not implemented for parallel calculations" $log; then
   echo "TEST ENDED AS EXPECTED"
   exit 0
fi

# SCF, MP2 and CCSD energies:
CRIT1=`$GREP "Final * HF energy:" $log | $GREP "\--100.008919(8|9)." | wc -l`
CRIT2=`$GREP "Total SCF   energy:                  \-100.008919(8|9)." $log | wc -l`
CRIT3=`$GREP "Total MP2   energy:                  \-100.193662(5|6)." $log | wc -l`
CRIT4=`$GREP "Total CCSD  energy:                  \-100.1977909." $log | wc -l`
TEST[1]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4` 
CTRL[1]=6
ERROR[1]="ENERGIES NOT CORRECT"

# CCSD gamma dispersion coefficients
# ZDIPLEN    ZDIPLEN    ZDIPLEN    ZDIPLEN        0    105.32914       105.32914       105.32914       105.32914
#                                                 2    5756.5661       2878.2830       1918.8554       959.42768    
# gamma_{||}                                      0    26.650035       26.650035       26.650035       26.650035
#                                                 2    1778.6517       889.32586       592.88391       296.44195  
# gamma_0   26.650035
# A         5.5617554   
CRIT1=`$GREP "0    105\.3291.       105\.3291.       105\.3291.       105\.3291." $log | wc -l`
CRIT2=`$GREP "2    5756\.566.       2878\.283.       1918\.855.       959\.427(6|7)." $log | wc -l`
CRIT3=`$GREP "0    26\.65003.       26\.65003.       26\.65003.       26\.65003." $log | wc -l`
CRIT4=`$GREP "2    1778\.65(1|2).       889\.325(8|9).       592\.88(39|40).       296\.44(19|20)." $log | wc -l`
CRIT5=`$GREP "gamma_0   26\.65003." $log | wc -l`
CRIT6=`$GREP "A         5\.56175(5|6)." $log | wc -l`
TEST[2]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3 + $CRIT4 \+ $CRIT5 \+ $CRIT6`
CTRL[2]=6
ERROR[2]="CCSD GAMMA DISPERSION COEFFICIENTS NOT CORRECT"

# CCSD static polarizability
# <<XDIPLEN ,XDIPLEN >>( 0.000000) =    -1.93065692
# <<YDIPLEN ,YDIPLEN >>( 0.000000) =    -1.93065692
# <<ZDIPLEN ,ZDIPLEN >>( 0.000000) =    -4.57091112                          
CRIT1=`$GREP "<<XDIPLEN ,XDIPLEN >>\( ( |0)\.000000\) =    \-1\.930656" $log | wc -l`
CRIT2=`$GREP "<<YDIPLEN ,YDIPLEN >>\( ( |0)\.000000\) =    \-1\.930656" $log | wc -l`
CRIT3=`$GREP "<<ZDIPLEN ,ZDIPLEN >>\( ( |0)\.000000\) =    \-4\.570911" $log | wc -l`
TEST[3]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[3]=3
ERROR[3]="CCSD STATIC POLARIZABILITIES NOT CORRECT"

# CCSD 1. hyperpolarizability
CRIT1=`$GREP "ZDIPLEN  \(unrel\.\) (\-0| \-|  | 0)\.[ 0]+ ZDIPLEN  \(unrel\.\)  ( |0)\.[ 0]+ ZDIPLEN  \(unrel\.\) (\-0| \-|  | 0)\.[ 0]+  15\.38085" $log | wc -l`
CRIT2=`$GREP "ZDIPLEN  \(unrel\.\) (\-0| \-|  | 0)\.[ 0]+ XDIPLEN  \(unrel\.\)  ( |0)\.[ 0]+ XDIPLEN  \(unrel\.\) (\-0| \-|  | 0)\.[ 0]+ ( |0)\.902737" $log | wc -l`
CRIT3=`$GREP "XDIPLEN  \(unrel\.\) (\-0| \-|  | 0)\.[ 0]+ XDIPLEN  \(unrel\.\)  ( |0)\.[ 0]+ ZDIPLEN  \(unrel\.\) (\-0| \-|  | 0)\.[ 0]+ ( |0)\.902737" $log | wc -l`
CRIT4=`$GREP "YDIPLEN  \(unrel\.\) (\-0| \-|  | 0)\.[ 0]+ ZDIPLEN  \(unrel\.\)  ( |0)\.[ 0]+ YDIPLEN  \(unrel\.\) (\-0| \-|  | 0)\.[ 0]+ ( |0)\.902737" $log | wc -l`
CRIT5=`$GREP "beta_\|\| * (\-0|\-| |0)\.0000 * (\-0|\-| |0)\.0000 * (\-0|\-| |0)\.0000     10\.311797" $log | wc -l`
CRIT6=`$GREP "beta\^K * (\-0|\-| |0)\.0000 * (\-0|\-| |0)\.0000 * (\-0|\-| |0)\.0000     10\.311797" $log | wc -l`
CRIT7=`$GREP "beta_\|_ * (\-0|\-| |0)\.0000 * (\-0|\-| |0)\.0000 * (\-0|\-| |0)\.0000     3\.437265(7|8)" $log | wc -l`
CRIT8=`$GREP "  beta_ms  " $log | sed "-e s/D/E/" -e "s/ -/  /g" | awk '{if (\$NF < 1.0E-8) print \$0}' | wc -l`
# hjaaj: with sed take absolute value of numbers because an awk version evaluated -0.1E-15 > -1.0E-8 as false :-(
TEST[4]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3 + $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ $CRIT7 \+ $CRIT8`
CTRL[4]=8
ERROR[4]="CCSD STATIC FIRST HYPERPOLARIZABILITIES (BETA) NOT CORRECT"

PASSED=1
for i in 1 2 3 4
do
   if [ ${TEST[i]} -ne ${CTRL[i]} ]; then
     echo "${ERROR[i]} ( test = ${TEST[i]}; control = ${CTRL[i]} ); "
     PASSED=0
   fi
done

if [ $PASSED -eq 1 ]
then
  echo TEST ENDED PROPERLY
  exit 0
else
  echo THERE IS A PROBLEM
  exit 1
fi                
%EOF%
#######################################################################
